﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Event_SG_EWDT
{
    public class EventControl
    {

        // When user wants to create event calls CreateEvent()
        // When user wants to update event calls UpdateEvent()

        #region Event

        public static string CreateEvent(string member_id, string EventTitle, string EventDescription, DateTime EventStartTime, DateTime EventEndTime, double EventFee, int categoriesID, string location, string Organiser, int privacy) //done
        {
            Event e = new Event();


            e.EventTitle = EventTitle;
            e.Description = EventDescription;
            e.StartTime = EventStartTime;
            e.EndTime = EventEndTime;
            //e.StartTime = DateTime.Now;
            //e.EndTime = DateTime.Now;
            e.CategoryId = categoriesID;
            e.EventFee = EventFee;
            e.Location = location;
            e.Organiser = Organiser;
            e.Privacy = privacy;
            e.EventId = DBMANAGER.CreateEvent(e);

           
            Event.listEvent.Add(e);
            CreateMemberEvent(e.EventId, member_id, "Admin", 0);

            return e.EventId;

        }
        public static List<Event> RetrieveEvent()  // Done
        {
            return DBMANAGER.RetrieveEvent();
        }
        public static Event RetrieveEvent(string eventID)
        {
            Event e = DBMANAGER.RetrieveEvent(eventID);

            return e;
        }
        public static bool UpdateEvent(string EventID, string EventTitle, string EventDescription, DateTime EventStartTime, DateTime EventEndTime, double EventFee, int CategoriesID, string Location, int Privacy)
        {
            Event e = new Event();
            e.EventId = EventID;
            e.EventTitle = EventTitle;
            e.Description = EventDescription;
            e.StartTime = EventStartTime;
            e.EndTime = EventEndTime;
            e.CategoryId = CategoriesID;
            e.EventFee = EventFee;
            e.Location = Location;
            e.Privacy = Privacy;

            bool up = DBMANAGER.UpdateEvent(e);
            return up;
        }
        public static int GetEventIndex_List(string eventID)
        {
            for (int i = 0; i < Event.listEvent.Count; i++)
            {
                if (Event.listEvent[i].EventId == eventID)
                {
                    return i;
                }
            }
            return -1;


        }
        public static Event GetEvent(string eventID)
        {
            for (int i = 0; i < Event.listEvent.Count; i++)
            {
                if (Event.listEvent[i].EventId == eventID)
                {
                    return Event.listEvent[i];
                }
            }
            return null;



        }

        #endregion


        //UpdateStatus or Role calls UpdateMemberEvent

        #region MemberEvent

        public static bool CreateMemberEvent(string EventID, string MemberShipNo, string MemberRole, int status)
        {
            MemberEvent ME = new MemberEvent();
            ME.EventId = EventID;
            ME.MembershipNo = MemberShipNo;
            ME.MemberRole = MemberRole;
            ME.Status = status;
            return DBMANAGER.AddMemberEvent(ME);

        }  //DONE
        public static List<MemberEvent> RetrieveMemberEvent(string MemberShipNO)
        {
            return DBMANAGER.RetrieveMemberEvent(MemberShipNO);
            // Retrieve from DB here
        }
        public static bool checkIfJoined(string membershipNo, string EventID)
        {
            List<MemberEvent> ListMemberEvent = DBMANAGER.RetrieveMemberEvent(membershipNo);
            for (int i = 0; i < ListMemberEvent.Count; i++)
            {
                if (ListMemberEvent[i].EventId == EventID)
                {
                    return true; // the member is exsit in the group
                    // edit this code please
                }

            }
            return false;
        }

        public static bool IsAdmin(string membershipNO ,string EventID)
        {
            List<MemberEvent> ListMemberEvent = DBMANAGER.RetrieveMemberEvent(membershipNO);
            for (int i = 0; i < ListMemberEvent.Count; i++)
            {
                if (ListMemberEvent[i].EventId == EventID)
                {
                    if (ListMemberEvent[i].MemberRole.ToLowerInvariant() == "admin")
                    {
                        return true; // the member is exsit in the group
                    }
                }

            }
            return false;
        }
        public static bool UpdateMemberEventStatus(string EventID, string MemberShipNo, int status)
        {
            try
            {
               MemberEvent me =  DBMANAGER.RetrieveMemberEvent(MemberShipNo,EventID)[0];
                
                me.Status = status;
                return DBMANAGER.UpdateMemberEvent(me);
            }
            catch
            {
                return false;
            }

        }
        public static bool UpdateMemberEvent_Role(string eventID, string memberShipNo, string role)
        {
            try
            {
                MemberEvent me = new MemberEvent();
                me.EventId = eventID;
                me.MembershipNo = memberShipNo;
                me.MemberRole = role;

              return DBMANAGER.UpdateMemberEvent_Role(me);   // untested query !!
            }
            catch
            {
                return false;
            }

        }
        //public static int GetMemberEventIndexFromTheList(string eventID, string membershipNo)
        //{
        //    for (int i = 0; i < MemberEvent.listMemberEvent.Count; i++)
        //    {
        //        if (MemberEvent.listMemberEvent[i].EventId == eventID && MemberEvent.listMemberEvent[i].MembershipNo == membershipNo)
        //        {
        //            return i;

        //        }
        //    }
        //    return -1;
        //}
        public static int FindAdmin(string eventID){
           return DBMANAGER.getAdminForEvent(eventID);
        }

        public static string GetCategoryName(int id)
        {
            return DBMANAGER.GetCategory(id)[0].CategoryName;
        }
        public static int RetrieveMemberEvent_Status(string MemberShipNO, string eventID)
        {
            return DBMANAGER.RetrieveMemberEvent_Status(MemberShipNO,eventID);
            // Retrieve from DB here
        }



        #endregion

        // Joining a Event calls JoinEvent
        // Leaving a Event calls LeaveEvent


        public static int JoinEvent(string memberid, string EventID)
        {

            Event ev = DBMANAGER.RetrieveEvent(EventID);
            int status = DBMANAGER.RetrieveMemberEvent_Status(memberid, EventID);   // 
            if (status == -1)
            {
                if (ev != null)
                {
                    int i = ev.Privacy;
                    if (i != -1)  // if the group is found in the list
                    {
                        return CheckEventPrivacy(memberid,EventID, i);

                    }
                }
            }
            else  // if he is in the waiting list  // Haven't tested yet !!
            {
                EventControl.UpdateMemberEventStatus(EventID, memberid, 0);
                return 0;
            }
            return -1;
        }
        public static int CheckEventPrivacy(string memberno, string EventID, int privacy)
        {
            if (privacy == 0)
            {
                CreateMemberEvent(EventID, memberno, "Member", 0);
                // status 0 is yes-attending
                return 0;
            }
            else if (privacy == 1)
            {
                CreateMemberEvent(EventID, memberno, "Member", 4);
                // status 4 is pending
                return 99;
            }
            return -1;
        }
        public static bool LeaveEvent(string member_no, string EventID)
        {
            MemberEvent me = new MemberEvent();
            me.EventId = EventID;
            me.MembershipNo = member_no;
            return DBMANAGER.DeleteMemberEvent(me);
        }


        //        status	description
        //        0	attending-yes
        //        1	attending-no
        //        2	attending-maybe
        //        3	attending-waiting
        //        4	attending-pending
        public static bool EventApproval(string membership_no, string eventID, int YesNoMaybe)
        {
            return UpdateMemberEventStatus(eventID, membership_no, YesNoMaybe);
        }
        public static bool invitePeople(HttpRequest request, string groupid, string eventid, string groupName, int memberid)
        {
            try
            {
                List<Member> L = DBQUERIES.RetrieveWhoIsNotInTheEventFromGroup(groupid, eventid);
                for (int i = 0; i < L.Count; i++)
                {
                    CreateMemberEvent(eventid, L[i].MembershipNo, "Member", 3);



                    Utility.EmailJoinGroupMessage(request, groupName, groupid, L[i].Email);

                    Utility.SendNotificationForEventInvite(request, eventid, memberid, Convert.ToInt32( L[i].MembershipNo));

                }

                // Prompt Notification Here!!!

                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}